package com.htc.android.footprints.activity;

import java.util.ArrayList;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.htc.android.footprints.overlay.AbstractGroupableOverlay;
import com.htc.android.footprints.provider.FootprintManager;
import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.utils.SettingInfo;
import com.htc.android.footprints.vo.Category;
import com.htc.android.footprints.vo.Footprint;

public class ShowOnMapActivity extends MapActivity
{
  private static final String TAG = "ShowOnMapActivity";
  private static Category curCategory;
  private static int zoomLevel;
  public ArrayList<Footprint> mDataList = null;
  private MapController mMapController = null;
  private DrawFootprintOverlay mMapOverlay = null;
  private MapView mMapView = null;
  private MyLocationOverlay mMyLocation;
  private TextView mNote = null;
  private TextView mTitleView;
  private boolean needMyLocation;

  private void checkLocationSources()
  {
    LocationManager localLocationManager = (LocationManager)getSystemService("location");
    if ((!localLocationManager.isProviderEnabled("gps")) || (!localLocationManager.isProviderEnabled("network")))
      new HtcAlertDialog.Builder(this).setTitle(getString(2131361938)).setIcon(17301543).setMessage(getString(2131361939)).setPositiveButton(2131361949, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          Intent localIntent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
          paramDialogInterface.dismiss();
          ShowOnMapActivity.this.startActivity(localIntent);
        }
      }).setNegativeButton(2131361796, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          paramDialogInterface.dismiss();
        }
      }).show();
  }

  private boolean networkConnected()
  {
    int i = 1;
    ConnectivityManager localConnectivityManager = (ConnectivityManager)getSystemService("connectivity");
    if (localConnectivityManager != null)
    {
      NetworkInfo localNetworkInfo = localConnectivityManager.getActiveNetworkInfo();
      if ((localNetworkInfo == null) || (localNetworkInfo.getState() != NetworkInfo.State.CONNECTED))
        break label37;
    }
    while (true)
    {
      return i;
      label37: i = 0;
    }
  }

  private void pasteNetworkDisabledText()
  {
    if (this.mNote == null)
    {
      this.mNote = new TextView(this);
      this.mNote.setText(2131361937);
      this.mNote.setTextColor(-13421773);
      this.mNote.setPadding(10, 5, 10, 5);
      this.mMapView.addView(this.mNote, new MapView.LayoutParams(-1, -2, 0, 0, 51));
    }
  }

  private void updateView()
  {
    Log.d("Sam", "curCategory " + curCategory.mKeyword);
    if ((curCategory == null) || (curCategory.equals(Category.getAllCategory().mKeyword)))
      this.mTitleView.setText(getResources().getString(2131361880));
    while (true)
    {
      FootprintQueryTask localFootprintQueryTask = new FootprintQueryTask(null);
      Category[] arrayOfCategory = new Category[1];
      arrayOfCategory[0] = curCategory;
      localFootprintQueryTask.execute(arrayOfCategory);
      return;
      this.mTitleView.setText(curCategory.mName);
    }
  }

  private int zoomToFitItems(Footprint[] paramArrayOfFootprint, Runnable paramRunnable)
  {
    int i4;
    if (paramArrayOfFootprint.length < 1)
      i4 = this.mMapView.getZoomLevel();
    while (true)
    {
      return i4;
      int i = 90000000;
      int j = -90000000;
      int k = 180000000;
      int m = -180000000;
      int n = paramArrayOfFootprint.length;
      for (int i1 = 0; i1 < n; i1++)
      {
        Footprint localFootprint = paramArrayOfFootprint[i1];
        int i5 = localFootprint.mLatitude;
        int i6 = localFootprint.mLongitude;
        j = Math.max(j, i5);
        i = Math.min(i, i5);
        m = Math.max(m, i6);
        k = Math.min(k, i6);
      }
      int i2 = (int)(1.4D * Math.abs(j - i));
      int i3 = (int)(1.2D * Math.abs(m - k));
      try
      {
        this.mMapController.zoomToSpan(i2, i3);
        this.mMapOverlay.changeZoom();
        GeoPoint localGeoPoint = new GeoPoint((j + i) / 2, (m + k) / 2);
        this.mMapController.animateTo(localGeoPoint);
        i4 = this.mMapView.getZoomLevel();
      }
      catch (OutOfMemoryError localOutOfMemoryError)
      {
        while (true)
        {
          Log.w("ShowOnMapActivity", localOutOfMemoryError.getMessage(), localOutOfMemoryError);
          System.gc();
        }
      }
    }
  }

  public void fitAll()
  {
    ArrayList localArrayList = this.mDataList;
    if (!localArrayList.isEmpty())
      zoomLevel = zoomToFitItems((Footprint[])localArrayList.toArray(new Footprint[localArrayList.size()]), null);
  }

  public boolean isEmpty()
  {
    if ((this.mDataList != null) && (!this.mDataList.isEmpty()));
    for (int i = 1; ; i = 0)
      return i;
  }

  protected boolean isRouteDisplayed()
  {
    return false;
  }

  public void moveTo(Footprint paramFootprint, Category paramCategory)
  {
    setCategory(paramCategory);
    this.mMapOverlay.jump(paramFootprint, this.mMapView);
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    getWindow().setBackgroundDrawable(null);
    setContentView(2130903077);
    this.mMapView = ((MapView)findViewById(2131493017));
    this.mMapController = this.mMapView.getController();
    zoomLevel = this.mMapView.getZoomLevel();
    LinearLayout localLinearLayout = (LinearLayout)findViewById(2131493018);
    LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(-2, -2);
    View localView = this.mMapView.getZoomControls();
    if ((localView instanceof ZoomControls))
    {
      ZoomControls localZoomControls = (ZoomControls)localView;
      localLinearLayout.addView(localZoomControls, localLayoutParams);
      localZoomControls.setOnZoomInClickListener(new View.OnClickListener()
      {
        public void onClick(View paramView)
        {
          try
          {
            ShowOnMapActivity.this.mMapController.zoomIn();
            return;
          }
          catch (OutOfMemoryError localOutOfMemoryError)
          {
            while (true)
            {
              Log.w("ShowOnMapActivity", localOutOfMemoryError.getMessage(), localOutOfMemoryError);
              System.gc();
            }
          }
        }
      });
      localZoomControls.setOnZoomOutClickListener(new View.OnClickListener()
      {
        public void onClick(View paramView)
        {
          try
          {
            ShowOnMapActivity.this.mMapController.zoomOut();
            return;
          }
          catch (OutOfMemoryError localOutOfMemoryError)
          {
            while (true)
            {
              Log.w("ShowOnMapActivity", localOutOfMemoryError.getMessage(), localOutOfMemoryError);
              System.gc();
            }
          }
        }
      });
    }
    this.mTitleView = ((TextView)findViewById(2131493016));
    this.mMapOverlay = new DrawFootprintOverlay(null);
    this.mMapView.getOverlays().add(this.mMapOverlay);
    this.mMyLocation = new MyLocationOverlay(this, this.mMapView);
    this.mMapView.getOverlays().add(this.mMyLocation);
    if ((!SettingInfo.getInstance(this).showConnectionFailedAlert) && (!networkConnected()))
      new Handler().postDelayed(new Runnable()
      {
        public void run()
        {
          ShowOnMapActivity.this.pasteNetworkDisabledText();
        }
      }
      , 500L);
    com.htc.android.footprints.commonui.TBarDrawable.mBoard = FootprintUtils.boundCenterBottom(getResources().getDrawable(2130837651));
    com.htc.android.footprints.commonui.TBarDrawable.mSelectedBoard = FootprintUtils.boundCenterBottom(getResources().getDrawable(2130837652));
    com.htc.android.footprints.commonui.TBarGroupDrawable.mBoard = FootprintUtils.boundCenterBottom(getResources().getDrawable(2130837648));
    com.htc.android.footprints.commonui.TBarGroupDrawable.mSelectedBoard = FootprintUtils.boundCenterBottom(getResources().getDrawable(2130837649));
    this.needMyLocation = false;
  }

  public void onDestroy()
  {
    super.onDestroy();
    Log.d("Sam", "onDestroy ");
    com.htc.android.footprints.commonui.TBarDrawable.mBoard = null;
    com.htc.android.footprints.commonui.TBarDrawable.mSelectedBoard = null;
    com.htc.android.footprints.commonui.TBarGroupDrawable.mBoard = null;
    com.htc.android.footprints.commonui.TBarGroupDrawable.mSelectedBoard = null;
    this.mMapOverlay.release();
    this.mMapView.getOverlays().clear();
    this.mDataList = null;
    this.mMapOverlay = null;
    if (this.mMyLocation != null)
      this.mMyLocation.disableMyLocation();
    this.mMyLocation = null;
  }

  protected void onPause()
  {
    super.onPause();
    zoomLevel = this.mMapView.getZoomLevel();
    getIntent().putParcelableArrayListExtra("display", null);
    getIntent().putExtra("category", (Parcelable)null);
    if (this.mMyLocation.isMyLocationEnabled())
      this.mMyLocation.disableMyLocation();
    System.gc();
  }

  public void onResume()
  {
    super.onResume();
    if (this.needMyLocation)
      this.mMyLocation.enableMyLocation();
    Intent localIntent = getIntent();
    Category localCategory = (Category)localIntent.getParcelableExtra("category");
    if (localCategory == null)
    {
      if (curCategory != null)
        localCategory = curCategory;
    }
    else
    {
      setCategory(localCategory);
      ArrayList localArrayList = localIntent.getParcelableArrayListExtra("display");
      if (localArrayList == null)
        break label126;
      zoomLevel = zoomToFitItems((Footprint[])localArrayList.toArray(new Footprint[localArrayList.size()]), null);
    }
    while (true)
    {
      if ((this.mNote != null) && (networkConnected()))
      {
        this.mMapView.removeView(this.mNote);
        this.mNote = null;
      }
      return;
      localCategory = Category.getAllCategory();
      break;
      label126: this.mMapController.setZoom(zoomLevel);
    }
  }

  public void setCategory(Category paramCategory)
  {
    curCategory = paramCategory;
    updateView();
  }

  public void showMyLocation()
  {
    checkLocationSources();
    if (!this.mMyLocation.isMyLocationEnabled())
    {
      this.mMyLocation.enableMyLocation();
      this.needMyLocation = true;
    }
    GeoPoint localGeoPoint = this.mMyLocation.getMyLocation();
    if (localGeoPoint == null)
    {
      Toast.makeText(this, 2131361977, 1000).show();
      this.mMyLocation.runOnFirstFix(new Runnable()
      {
        public void run()
        {
          ShowOnMapActivity.this.mMapView.getController().animateTo(ShowOnMapActivity.this.mMyLocation.getMyLocation());
        }
      });
    }
    while (true)
    {
      return;
      this.mMapView.getController().animateTo(localGeoPoint);
    }
  }

  private class FootprintQueryTask extends AsyncTask<Category, Integer, ArrayList<Footprint>>
  {
    private FootprintManager mManager;

    private FootprintQueryTask()
    {
    }

    protected ArrayList<Footprint> doInBackground(Category[] paramArrayOfCategory)
    {
      if (this.mManager == null)
        this.mManager = FootprintManager.getInstance(ShowOnMapActivity.this.getApplicationContext());
      ArrayList localArrayList2;
      if (paramArrayOfCategory != null)
      {
        Cursor localCursor = this.mManager.findFootprintCursorByCategoryAndKeyword(paramArrayOfCategory[0].mKeyword, null);
        localArrayList2 = new ArrayList(localCursor.getCount());
        while (localCursor.moveToNext())
        {
          Footprint localFootprint = new Footprint();
          FootprintManager.fillDataFromCursor(localFootprint, localCursor);
          localArrayList2.add(localFootprint);
        }
        localCursor.close();
      }
      for (ArrayList localArrayList1 = localArrayList2; ; localArrayList1 = null)
        return localArrayList1;
    }

    protected void onPostExecute(ArrayList<Footprint> paramArrayList)
    {
      ShowOnMapActivity.this.mDataList = paramArrayList;
      if (ShowOnMapActivity.this.mMapOverlay != null)
        ShowOnMapActivity.this.mMapOverlay.populateData();
      ShowOnMapActivity.this.mMapView.invalidate();
    }
  }

  private class DrawFootprintOverlay extends AbstractGroupableOverlay
  {
    private DrawFootprintOverlay()
    {
    }

    protected Footprint createItem(int paramInt)
    {
      return (Footprint)ShowOnMapActivity.this.mDataList.get(paramInt);
    }

    protected boolean onTap(Footprint[] paramArrayOfFootprint)
    {
      if (paramArrayOfFootprint.length == 1)
      {
        Intent localIntent = new Intent("com.htc.android.footprints.activity.DetailViewActivity");
        localIntent.putExtra("DATA", paramArrayOfFootprint[0]);
        localIntent.addFlags(4194304);
        localIntent.addFlags(67108864);
        localIntent.putExtra("EXTRA_SORT_BY", "create_time desc");
        localIntent.putExtra("EXTRA_CATEGORY", ShowOnMapActivity.curCategory.mKeyword);
        localIntent.putExtra("EXTRA_ITEM_IDX", -1);
        ShowOnMapActivity.this.startActivity(localIntent);
      }
      while (true)
      {
        return true;
        if (paramArrayOfFootprint.length <= 1)
          continue;
        if (ShowOnMapActivity.zoomLevel == ShowOnMapActivity.this.mMapView.getMaxZoomLevel())
        {
          String[] arrayOfString = new String[paramArrayOfFootprint.length];
          for (int i = 0; i < paramArrayOfFootprint.length; i++)
            arrayOfString[i] = paramArrayOfFootprint[i].mName;
          new HtcAlertDialog.Builder(ShowOnMapActivity.this).setTitle(2131361966).setItems(arrayOfString, new DialogInterface.OnClickListener(paramArrayOfFootprint)
          {
            public void onClick(DialogInterface paramDialogInterface, int paramInt)
            {
              Intent localIntent1 = ShowOnMapActivity.this.getIntent();
              Intent localIntent2 = new Intent("com.htc.android.footprints.activity.DetailViewActivity");
              localIntent2.putExtra("DATA", this.val$footprints[paramInt]);
              localIntent2.putExtra("EXTRA_SORT_BY", localIntent1.getStringExtra("EXTRA_SORT_BY"));
              localIntent2.putExtra("EXTRA_CATEGORY", localIntent1.getStringExtra("EXTRA_CATEGORY"));
              localIntent2.addFlags(4194304);
              localIntent2.addFlags(67108864);
              localIntent2.putExtra("EXTRA_SORT_BY", "create_time desc");
              localIntent2.putExtra("EXTRA_CATEGORY", ShowOnMapActivity.curCategory.mKeyword);
              localIntent2.putExtra("EXTRA_ITEM_IDX", -1);
              ShowOnMapActivity.this.startActivity(localIntent2);
            }
          }).show();
          continue;
        }
        ShowOnMapActivity.access$502(ShowOnMapActivity.this.zoomToFitItems(paramArrayOfFootprint, null));
      }
    }

    public void populateData()
    {
      super.populate();
    }

    public int size()
    {
      if (ShowOnMapActivity.this.mDataList == null);
      for (int i = 0; ; i = ShowOnMapActivity.this.mDataList.size())
        return i;
    }
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.activity.ShowOnMapActivity
 * JD-Core Version:    0.6.0
 */